import { Component, OnInit } from '@angular/core'
import { FormBuilder } from '@angular/forms'
import { NzMessageService } from 'ng-zorro-antd'

import { StoreNewBaseClass } from '../baseClass'
import { StoreDataService } from '../../store-data.service'
import { makeAllControlDirty } from 'app/utils/helpers'
import { I18NService } from '@core'

@Component({
  selector: 'app-store-new-qrcode-func',
  templateUrl: './qrcode-func.component.html',
})
export class StoreNewQrcodeFuncComponent extends StoreNewBaseClass implements OnInit {
  constructor(
    public fb: FormBuilder,
    public storeService: StoreDataService,
    private i18n: I18NService,
    private msg: NzMessageService
  ) {
    super(fb, storeService)
  }

  ngOnInit() {
    this.initValidateForm()
  }

  initValidateForm(): void {
    const { isRefundAsync, isSupportTips } = this.store.store.qr
    this.validateForm = this.fb.group({
      isRefundAsync: [`${isRefundAsync}`, []],
      isSupportTips: [isSupportTips, []],
    })
  }

  pushDataToStore(): void {
    const { qr } = this.store.store
    const tmp = this.validateForm.value
    tmp.isRefundAsync = tmp.isRefundAsync === 'true' ? true : false
    Object.assign(qr, tmp)
    console.log('qr', qr)
    this.storeStore.updateValue(this.store)
  }

  checkValid(): boolean {
    if (this.validateForm.valid) {
      this.pushDataToStore()
      return true
    }
    makeAllControlDirty(this.validateForm)
    this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
    return false
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
